package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {

    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            String uuid = UUID.randomUUID().toString();
            company.setId(uuid);
            mapper.save(company);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            mapper.delete(id);
            TransactionUtil.commit(sqlSession);
            } catch (Exception e) {
                TransactionUtil.rollback(sqlSession);
                throw new RuntimeException();
            } finally {
                TransactionUtil.close(sqlSession);
            }
    }

        @Override
        public void update (Company company){
            SqlSession sqlSession = null;
            try {
                sqlSession = MapperFactory.getSqlSession();
                CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
                mapper.update(company);
                TransactionUtil.commit(sqlSession);
            } catch (Exception e) {
                TransactionUtil.rollback(sqlSession);
                throw new RuntimeException();
            } finally {
                TransactionUtil.close(sqlSession);
            }
        }

        @Override public Company findById (String id){
            SqlSession sqlSession = null;
            try {
                sqlSession = MapperFactory.getSqlSession();
                CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
                Company byId = mapper.findById(id);
                TransactionUtil.commit(sqlSession);
                return byId;
            } catch (Exception e) {
                TransactionUtil.rollback(sqlSession);
                throw new RuntimeException();
            } finally {
                TransactionUtil.close(sqlSession);
            }
        }

        @Override
        public List<Company> findAll ( int page, int size){
            SqlSession sqlSession=null;
            try {
             sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            PageHelper.startPage(page, size);
            List<Company> all = mapper.findAll();
            TransactionUtil.commit(sqlSession);
            return all;
            } catch (Exception e) {
                TransactionUtil.rollback(sqlSession);
                throw new RuntimeException();
            } finally {
                TransactionUtil.close(sqlSession);
            }
        }

    @Override
    public List<Company> findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            CompanyDao mapper = sqlSession.getMapper(CompanyDao.class);
            List<Company> all = mapper.findAll();
            TransactionUtil.commit(sqlSession);
            return all;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
